#ifndef ZHY_MUDUO_BASE_CURRENTTHREAD_H
#define ZHY_MUDUO_BASE_CURRENTTHREAD_H

#include <cstdint>

namespace _zhy_muduo
{
namespace CurrentThread
{

#if (defined(_MSC_VER))
	typedef unsigned long tid_t;
#elif (defined(__GNUC__))
	typedef int tid_t;
#endif

void cacheTid();

#if (defined(_MSC_VER))
extern __declspec(thread) tid_t t_cachedTid;
extern __declspec(thread) char t_tidString[32];
extern __declspec(thread) int t_tidStringLength;
extern __declspec(thread) const char* t_threadName;

#define __builtin_expect(EXP, C)  (EXP)
inline tid_t tid() 
{
	if (__builtin_expect(t_cachedTid == 0, 0)) {
		cacheTid();
	}
	return t_cachedTid;
}
#elif (defined(__GNUC__))
extern __thread int t_cachedTid;
extern __thread char t_tidString[32];
extern __thread int t_tidStringLength;
extern __thread const char* t_threadName;

inline int tid()
{
	if (__builtin_expect(t_cachedTid == 0, 0)) {
		cacheTid();
	}
	return t_cachedTid;
}
#endif

inline const char* tidString() // for logging
{
	return t_tidString;
}

inline int tidStringLength() // for logging
{
	return t_tidStringLength;
}

inline const char* name()
{
	return t_threadName;
}

bool isMainThread();

void sleepUsec(int64_t usec);
}//end namespace CurrentThread
}//end namespace _zhy_muduo

#endif //ZHY_MUDUO_BASE_CURRENTTHREAD_H